package com.bot.devbot.service.feishu;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 企业微信机器人接口封装
 * <a href="https://developer.work.weixin.qq.com/document/path/91770">企业微信机器人</a>
 * > 需要\n\n <@%s>
 * <a href="https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot?lang=zh-CN">企业微信机器人文档</a>
 *
 * @author xialiguo
 * @version 1.0
 * @date 2025/1/21 15:41
 * @since 1.0
 */
@Service
public class FeiShuService {

    // 夏利国个人助手
    private String feiShuWebhook = "https://open.feishu.cn/open-apis/bot/v2/hook/5e18ae7c-4421-455f-8a41-a07b86929736";
    // hook集合
    private String feiShuWebhook2 = "https://open.feishu.cn/open-apis/bot/v2/hook/e1de4821-9048-4cf1-aac5-b4136a1132b5";
    // 集合 包含feiShuWebhook feiShuWebhook2
    private List<String> feiShuWebhookList = Arrays.asList();

    // Nexus通知-个人
    private String feiShuWebhook3 = "https://open.feishu.cn/open-apis/bot/v2/hook/4ef4495e-4f83-418f-b7ca-f943e7041afd";

    List<String> myCaredProjects = Arrays.asList(
            "charge-platform",
            "framework"
    );



    public void sendMessage(String message) {
        JSONObject body = new JSONObject();
        body.put("msg_type", "text");
        JSONObject text = new JSONObject();
        text.put("text", message);
        body.put("content", text);

        HttpRequest.post(feiShuWebhook)
                .body(body.toString())
                .execute();
    }

    public void sendMessagePost(String projectName, String branchName, String message, String url) {
        JSONObject body = new JSONObject();
        body.set("msg_type", "post");
        JSONObject content = new JSONObject();
        JSONObject post = new JSONObject();
        JSONObject zh_cn = new JSONObject();
        JSONArray zh_cnContent = new JSONArray();
        JSONArray zh_cnContent2 = new JSONArray();
        zh_cnContent.add(0, zh_cnContent2);
        JSONObject zh_cnContentItem = new JSONObject();
        JSONObject zh_cnContentItem2 = new JSONObject();
        zh_cnContentItem.set("tag", "text");
        zh_cnContentItem.set("text", message);
        zh_cnContentItem2.set("tag", "a");
        zh_cnContentItem2.set("text", "\n点击查看变更");
        zh_cnContentItem2.set("href", url + "?view=parallel");
        zh_cnContent2.add(0, zh_cnContentItem);
        zh_cnContent2.add(1, zh_cnContentItem2);
        body.set("content", content);
        content.set("post", post);
        post.set("zh_cn", zh_cn);
        zh_cn.set("title", "GitLab变更通知");
        zh_cn.set("content", zh_cnContent);
        // 打印body
        System.out.println(body);

        for (String hookUrl : feiShuWebhookList) {
            HttpResponse response = HttpRequest.post(hookUrl)
                    .body(body.toString())
                    .execute();
        }
        if (myCaredProjects.contains(projectName)) {

        }
        HttpResponse response = HttpRequest.post(feiShuWebhook)
                .body(body.toString())
                .execute();
    }

    public void sendMessagePostNexus(String message, String url) {
        JSONObject body = new JSONObject();
        body.set("msg_type", "post");
        JSONObject content = new JSONObject();
        JSONObject post = new JSONObject();
        JSONObject zh_cn = new JSONObject();
        JSONArray zh_cnContent = new JSONArray();
        JSONArray zh_cnContent2 = new JSONArray();
        zh_cnContent.add(0, zh_cnContent2);
        JSONObject zh_cnContentItem = new JSONObject();
        JSONObject zh_cnContentItem2 = new JSONObject();
        zh_cnContentItem.set("tag", "text");
        zh_cnContentItem.set("text", message);
        zh_cnContentItem2.set("tag", "a");
        zh_cnContentItem2.set("text", "\n点击查看变更");
        zh_cnContentItem2.set("href", url);
        zh_cnContent2.add(0, zh_cnContentItem);
        zh_cnContent2.add(1, zh_cnContentItem2);
        body.set("content", content);
        content.set("post", post);
        post.set("zh_cn", zh_cn);
        zh_cn.set("title", "Nexus仓库变更通知");
        zh_cn.set("content", zh_cnContent);
        // 打印body
        System.out.println(body);
        HttpResponse response = HttpRequest.post(feiShuWebhook3)
                .body(body.toString())
                .execute();
    }

}
